package com.stella.comeon.services;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stella.comeon.entity.Book;
import com.stella.comeon.mapper.BookMapper;
import com.stella.comeon.web.User;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class BookService extends ServiceImpl<BookMapper, Book> {

    public void parsePdfBookToImg() {
        try {
            SpringUtil.getBean(PdfProcessingService.class).processStages();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public void saveBook(Integer level, String path, String imgPath, String bookName, String shortName) {
        Book book = new Book();
        book.setPath(path);
        book.setBookName(bookName);
        book.setShortName(shortName);
        book.setLevel(level);
        book.setImgPath(imgPath);
        this.save(book);

    }

    public IPage<Book> queryPage(Integer level) {
        IPage<Book> bookPage = new PageDTO<>(1, 20);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (level != null) {
            queryWrapper.eq("level", level);
        }
        return this.page(bookPage, queryWrapper);
    }
}
